package cn.rayland.rayland3d.activity;

import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;

import cn.rayland.library.bean.MachineState;

/**
 * Created by gw on 2016/5/3.
 */
public class WebSocketPusher {
    private static final String TAG = WebSocketPusher.class.getSimpleName();
    private static WebSocketClient client;
    private static Timer timer;
    private static TimerTask task;
    private static MachineState machineState;
    public static void startPush(final String url, final long delay, final MachineState data) {
        new Thread(){
            @Override
            public void run() {
                try {
                    timer = new Timer();
                    machineState = new MachineState();
                    machineState.setVal(data.getVal());
                    machineState.setTemper(data.getTemper());
                    machineState.setProgress(data.getProgress());
                    machineState.setHeating(data.getHeating());
                    machineState.setUsedTime(data.getUsedTime());
                    machineState.setFanning(data.getFanning());
                    URI uri = new URI(url);
                    task = new TimerTask() {
                        @Override
                        public void run() {
                            client.send(JSON.toJSONString(data));
                            Log.d(TAG, "send" + JSON.toJSONString(data));
                        }
                    };

                    client = new WebSocketClient(uri, new Draft_17()) {
                        @Override
                        public void onOpen(ServerHandshake handshakedata) {
                            Log.d(TAG, "Opened");
                            timer.schedule(task, 0, delay);

                        }

                        @Override
                        public void onMessage(String message) {
                            Log.d(TAG, "onMessage = " + message);
                        }

                        @Override
                        public void onClose(int code, String reason, boolean remote) {
                            Log.d(TAG, "Closed");
                            timer.cancel();
                        }

                        @Override
                        public void onError(Exception ex) {
                            Log.i(TAG, "Error " + ex.getMessage());
                            timer.cancel();
                        }
                    };
                    client.connect();
                    Log.d(TAG, "Try connect");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    public static void stopPush() {
        new Thread(){
            @Override
            public void run() {
                try {
                    if (null != client) {
                        timer.cancel();
                        client.send("Print is over");
                        Log.d(TAG, "send" +"Print is over");
                        client.close();
                        Log.d(TAG, "Try close");
                        client = null;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
